//
//  SLStatusToolBar.swift
//  Sina15
//
//  Created by 盼 on 16/4/13.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import SnapKit

class SLStatusToolBar: UIView {
    
    //声明3个按钮变量
    var forwardBtn: UIButton!
    var commentBtn: UIButton!
    var favorBtn:UIButton!
    
    //在模型中绑定数据
    var statusModel: SLStatusViewModel?{
        
        didSet {
            forwardBtn.setTitle(statusModel?.forwardTitle, forState: .Normal)
            
            commentBtn.setTitle(statusModel?.commentTitle, forState: .Normal)
            
            favorBtn.setTitle(statusModel?.favorTitle, forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置工具条界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI(){
        
        
       forwardBtn = addChildBtn("转发", imageName: "timeline_icon_retweet")
      commentBtn = addChildBtn("评论", imageName: "timeline_icon_comment")
      favorBtn = addChildBtn("赞", imageName: "timeline_icon_comment")
        
        //设置约束
        forwardBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(self.snp_left)
        }
        
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.width.equalTo(forwardBtn)
            make.left.equalTo(forwardBtn.snp_right)
        }
        
        favorBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.width.equalTo(commentBtn)
            make.left.equalTo(commentBtn.snp_right)
            make.right.equalTo(self.snp_right)
        }
        
        //三个按钮三等分视图
        let line1 = setupLine()
        let line2 = setupLine()
        
        //把线的宽度设置成一个像素点
        let w = 1 / UIScreen.mainScreen().scale
        
        let mutiplied = 0.4
        
        //设置约束
        line1.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(mutiplied)
            make.left.equalTo(forwardBtn.snp_right)
            make.width.equalTo(w)
        }
        
        line2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(mutiplied)
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(w)
        }
    }
    

    //设置分割线
    func setupLine()->UIView {
        let line = UIView()
        line.backgroundColor = UIColor.purpleColor()
        
        addSubview(line)
        return line
    }
    
    func addChildBtn(textTitle: String,imageName: String)->UIButton {
        let btn = UIButton()
        //设置title
        btn.setTitle(textTitle, forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_middle_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_middle_background_highlighted"), forState: .Highlighted)
        
        //设置自适应大小
        btn.sizeToFit()
        
        //添加到toolBar上
        addSubview(btn)
        
        return btn
    }

}
